package xyz.xtt.exchange.controller;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import xyz.xtt.exchange.vo.User;

@RestController
public class FileUploadController {

	private final Path root = Paths.get("uploads"); // 上传文件的目录

	@PostMapping("/upload")
	public String uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("users") List<User> users) {
		try {
			// 检查上传目录是否存在，如果不存在则创建
			if (!Files.exists(root)) {
				Files.createDirectories(root);
			}
			// 保存文件到上传目录
			Files.copy(file.getInputStream(), root.resolve(file.getOriginalFilename()));

			// 处理接收的用户列表
			for (User user : users) {
				System.out.println("Received user: " + user);
			}

			return "File uploaded successfully and users processed.";
		} catch (IOException e) {
			e.printStackTrace();
			return "Failed to upload file or process users.";
		}
	}
}
